<template>
	<view class="container">
		<view class="visitor">
			<view class="outside">
				<view class="within">
					<view class="info">
						<view class="name">
							<text>{{infoData.name}}</text>
							<!-- <text class="classType">【门诊】</text> -->
							<view class="btn m-l-30" @click="showVisitCard=true">
								<img src="@/static/images/qiehuan.png" style="width:29rpx;margin-right:8rpx;"></img>
								切换就诊人
							</view>
						</view>
						<view class="cord">
							卡号
						</view>
						<view>{{$desensitizationNormalStr(infoData.cardNum)}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="screenType">
			<view class="typeLeft">
				<uni-icons class="uni-icons" type="calendar" size="22"></uni-icons>
				<uni-datetime-picker type="date" :end="endTime" v-model="beginTime"
					@change="beginChange($event)">{{beginTime}}</uni-datetime-picker>
				<view style="margin: 0 10rpx;">-</view>
				<uni-datetime-picker type="date" :start="beginTime" v-model="endTime"
					@change="endChange($event)">{{endTime}}
				</uni-datetime-picker>
			</view>
		</view>
		<view style="padding-left: 24rpx;padding-right: 24rpx;">
			<u-tabs :list="list1" :activeStyle="{
            color: '#303133',
            fontWeight: 'bold',
            transform: 'scale(1.05)'
        }" :inactiveStyle="{
            color: '#606266',
            transform: 'scale(1)'
        }" @click="changeCheckType"></u-tabs>
		</view>

		<view class="noCard" v-if="dataList.length<1">
			<view class="images">
				<img src="@/static/images/noCard.png" alt="">
				<view>
					未查询到信息
				</view>
			</view>
		</view>

		<view class="list" v-else>
			<scroll-view class="scroll-view_H" scroll-y="true" style="height: calc(100vh - 420rpx);">
				<view class="items" v-for="(item,index) in dataList" :key="index" @click="goDtl(item)">
					<view class="title">
						<text>{{item.itemName}}</text>
					</view>
					<view class="icd">
						<text class="left">临床诊断</text>
						<text>{{item.diagnosis}}</text>
					</view>
					<view class="name">
						<text class="left">报告时间</text>
						<text>{{item.checkDate != null && item.checkDate != '' ?item.checkDate.substring(0,19):''}}</text>
					</view>
					<view class="dept">
						<text class="left">报告状态</text>
						<text>{{item.status}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<liuCalendarDate ref="calendar2" isRange @checked='changeTime'></liuCalendarDate>
		<!-- 切换就诊人 -->
		<u-popup :show="showVisitCard" :round="10" :safeAreaInsetBottom="true" mode="bottom">
			<switchVisitCard :visitData="visitList" :relations="relationTrans" @closeVisit="close" />
		</u-popup>
	</view>
</template>

<script>
	import {
		getPatientBaseList,
		patientBase,
		getInfoMatch
	} from "@/api/card/index.js"
	import switchVisitCard from '@/components/switchVisitCard/index.vue';
	import liuCalendarDate from '@/components/liu-calendar-date/liu-calendar-date.vue';
	import {
		getDictType
	} from "@/api/system/public.js";
	import {
		getInspectResultList
	} from "@/api/beInHospital/index.js";
	import {
		EventBus
	} from "@/utils/event-bus.js";
	import {
		getConfigKey,
		getVisitorList
	} from "@/api/home/home.js";
	import {
		getQueryString,
		idCard
	} from "@/utils/common.js";
	export default {
		components: {
			switchVisitCard,
			liuCalendarDate
		},
		data() {
			return {
				infoData: {},
				timesRange: '全部时间',
				showTime: false,
				visitList: [],
				showVisitCard: false,
				relationTrans: {},
				dataList: [],
				beginTime: '',
				endTime: '',
				checkType: '2',
				list1: [{
					name: '检查报告',
					id: '2'
				}, {
					name: '检验报告',
					id: '1'
				}],
				openid: ''
			}
		},
		onLoad(options) {
				/*
			let openid = uni.getStorageSync('openid') || ''
			this.openid = openid
			let _that = this
			EventBus.$on('report', function(res) {
				_that.getCardList()
				_that.infoData = JSON.parse(uni.getStorageSync('currentVisitCard'))
			})
			this.getConfigOutSideJump()
			if (uni.getStorageSync('currentVisitCard')) {
				this.infoData = JSON.parse(uni.getStorageSync('currentVisitCard'))
			}
			let oneDay = 24 * 60 * 60 * 1000
			this.beginTime = this.$parseTime(new Date(Date.now() - 30 * oneDay), '{y}-{m}-{d}')
			this.endTime = this.$parseTime(new Date(), '{y}-{m}-{d}')
			this.getCardList()
			this.getDict()
			this.getInspectResultList()*/
		},
		mounted(){
			let openid = uni.getStorageSync('openid') || ''
			this.openid = openid
			let _that = this
			EventBus.$on('report', function(res) {
				_that.getCardList()
				_that.infoData = JSON.parse(uni.getStorageSync('currentVisitCard'))
			})
			this.getConfigOutSideJump()
			if (uni.getStorageSync('currentVisitCard')) {
				this.infoData = JSON.parse(uni.getStorageSync('currentVisitCard'))
			}
			let oneDay = 24 * 60 * 60 * 1000
			this.beginTime = this.$parseTime(new Date(Date.now() - 30 * oneDay), '{y}-{m}-{d}')
			this.endTime = this.$parseTime(new Date(), '{y}-{m}-{d}')
			this.getCardList()
			this.getDict()
			this.getInspectResultList()
		},
		onPullDownRefresh() {
			this.getInspectResultList()
		},
		methods: {
			/* 是否开启对接外部跳转 */
			getConfigOutSideJump() {
				getConfigKey('sys_outside_link_jump').then(res => {
					if (res.msg == '1') {
						if (uni.getStorageSync('userId') != '' && this.openid) {
							this.jzswjw(uni.getStorageSync('userId'))
						}
					} else {
						uni.removeStorageSync('userId')
						uni.removeStorageSync('jzjktFunc')
					}
				});
			},
			/* 回调接口获取用户信息 */
			jzswjw(userid) {
				let _that = this
				uni.request({
					url: 'https://jkt.jzswjw.gov.cn/AREAServer/user',
					method: 'GET',
					data: {
						userId: userid
					},
					success: (res) => {
						_that.judgeIsNoCreateCard(res.data.data)
					},
					fail: () => {}
				})

			},
			/* 判断回调来的用户是否已建卡 */
			judgeIsNoCreateCard(params) {
				getVisitorList({
					hospitalCode: JSON.parse(uni.getStorageSync('hospital')).hospitalCode,
					openId: this.openid
				}).then(res => {
					console.log(res, '---就诊卡列表-----')
					/* 人员列表 */
					if (res.rows.length > 0) { //用户已建卡
						EventBus.$emit('loadVisitor', '')
						uni.removeStorageSync('userId')
						uni.removeStorageSync('jzjktFunc')
					} else { //用户未建卡默认建卡
						this.autoCreatCard(params)
					}
				});
			},
			/* 自动建卡 */
			autoCreatCard(info) {
				if (uni.getStorageSync('openid')) {
					let params = {
						address: '',
						headImg: this.$store.state.user.avatar,
						hospitalCode: JSON.parse(uni.getStorageSync('hospital')).hospitalCode,
						idNum: info.idNo,
						mobile: info.mobile,
						name: info.name,
						nation: '',
						nickName: this.$store.state.user.name,
						openId: this.openid,
						relation: '1',
						relationName: '本人',
						sex: idCard(info.idNo, 2) == '男' ? '1' : idCard(info.idNo, 2) == '女' ? '2' : ''
					}
					getInfoMatch({
						name: info.name,
						idNum: info.idNo
					}).then(ress => {
						if (ress.data && ress.data.patientCard) {
							params.cardNum = ress.data.patientCard
							params.feeType = ress.data.mediTreatement
							params.cardStatu = ress.data.cardType
							params.feeBalance = ress.data.cardBalance
						}
						patientBase(params).then(res => {
							console.log(res, '---建卡成功呢-----')
							if (res.code == 200) {
								EventBus.$emit('loadVisitor', '')
								this.setCurrentCard()
								uni.removeStorageSync('userId')
								uni.removeStorageSync('jzjktFunc')
							}
						}).catch(errors => {});
					}).catch(errors => {});
				}
			},
			/* 设置当前卡 */
			setCurrentCard() {
				getVisitorList({
					hospitalCode: JSON.parse(uni.getStorageSync('hospital')).hospitalCode,
					openId: this.openid
				}).then(res => {
					if (res.rows.length > 0) { //用户已建卡
						uni.setStorageSync('currentVisitCard', JSON.stringify(res.rows[0]))
						this.infoData = JSON.parse(uni.getStorageSync('currentVisitCard'))
					}
				});
			},
			getCardList() {
				let params = {
					hospitalCode: JSON.parse(uni.getStorageSync('hospital')).hospitalCode,
					openId: this.openid,
				}
				getPatientBaseList(params).then(res => {
					if (res.code == 200) {
						this.visitList = res.rows
					}
				})
			},
			getInspectResultList() {
				let obj = {
					"name": this.infoData.name,
					"cardNum": this.infoData.cardNum || this.infoData.idNum,
					"idNum": this.infoData.idNum,
					"startTime": this.beginTime,
					"endTime": this.endTime,
					"checkType": this.checkType, //0-全部 1-检验 2-检查
				}
				getInspectResultList(obj).then(res => {
					//数据请求完成之后停止下拉刷新
					uni.stopPullDownRefresh();
					this.dataList = res.data || []
				})
			},
			goDtl(item) {
				uni.navigateTo({
					url: '/pages/mine/report/dtlView?id=' + item.queryId + '&checkType=' + item.checkType
				})
			},
			close() {
				this.showVisitCard = false
				EventBus.$emit('loadVisitor', '')
				if (uni.getStorageSync('currentVisitCard')) {
					this.infoData = JSON.parse(uni.getStorageSync('currentVisitCard'))
				}
				this.getInspectResultList()
			},
			/* 开始日期 */
			beginChange(e) {
				this.getInspectResultList()
			},
			/* 结束日期 */
			endChange(e) {
				this.getInspectResultList()
			},
			changeCheckType(item) {
				this.checkType = item.id
				this.getInspectResultList()
			},
			/* 字典 */
			getDict() {
				getDictType({
					dictType: 'sys_relation'
				}).then(res => {
					if (res.code == 200) {
						res.rows.forEach(item => {
							this.relationTrans[item.dictValue] = item.dictLabel
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: rgb(245, 245, 245);
	}

	.noCard {
		width: 100%;
		text-align: center;
		color: #666666;
		padding: 24rpx 0 0 0;
		margin-top: 72rpx;
		color: #808080;

		.images {
			width: 100%;
			margin-bottom: 40rpx;
			color: #666666;

			img {
				width: 55%;
				text-align: center;
			}
		}
	}

	.list {
		padding: 24rpx;
		margin: 0 auto;

		.items {
			background-color: #fff;
			border-radius: 8px;
			margin-bottom: 24rpx;
			padding-bottom: 12rpx;

			.title {
				padding: 24rpx;
				display: flex;
				font-size: 28rpx;
				font-weight: 600;
				align-items: center;
				border-bottom: 1rpx solid #eee;
				margin-bottom: 12rpx;

				.btn {
					font-size: 24rpx;
					border: 1rpx solid #D2E3FF;
					font-weight: 400;
					border-radius: 4px;
					margin-left: 24rpx;
					padding: 4rpx 9rpx;
					background-color: #D2E3FF;
					color: rgb(92, 147, 249);
				}
			}

			.name,
			.icd,
			.time,
			.dept {
				display: flex;
				justify-content: space-between;
				padding: 12rpx 24rpx;
				font-size: 28rpx;

				.left {
					color: #999;
					width: 130rpx;
				}

				.icdtext {
					flex: 1;
					color: #FF6430;
					text-align: right;
				}
			}
		}
	}

	.screenType {
		width: 100%;
		display: flex;
		align-items: center;
		padding: 20rpx;
		background-color: rgb(92, 147, 249);
		justify-content: space-around;
		color: #fff;
		
		.typeLeft,
		.typeRight {
			display: flex;
			align-items: center;
			font-size: 16px;
			font-weight: 600;
		}
		.typeLeft{
			// padding: 2px 8px;
			// border: 1px solid rgba(255,255,255,0.3);
		}
		.uni-icons{color: #fff!important;}
	}

	.visitor {
		width: 100%;
		// background-color: rgb(92, 147, 249);
		background: url('@/static/images/report-small-bg.png') no-repeat left center, linear-gradient(180deg, rgba(197, 218, 255, 1) 0%, rgba(92, 147, 249, 1) 100%);
		background-size: auto 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 24rpx 24rpx 0 24rpx;
		color: #3C537D;

		.outside {
			width: 100%;
			background-color: #fff;
			padding: 12rpx 12rpx 0 12rpx;
			border-radius: 8px 8px 0 0;
			background: url('@/static/images/card_bg.png') no-repeat;
			background-size: 100% 100%;

			.within {
				border-radius: 8px 8px 0 0;

				.info {
					padding: 16rpx 28rpx 20rpx;
					font-size: 34rpx;

					.name {
						display: flex;
						align-items: center;

						.classType {
							display: inline-block;
							margin: 0 36rpx;
						}

						.btn {
							font-size: 28rpx;
							padding: 7rpx 14rpx;
							background: linear-gradient(90deg, rgba(77, 142, 255, 1) 0%, rgba(94, 150, 247, 1) 100%);
							color: #fff;
							display: flex;
							align-items: center;
							border-radius: 5px;
						}
					}

					.cord {
						font-size: 28rpx;
						padding: 16rpx 0 8rpx;
					}
				}
			}
		}
	}
</style>